<template>
	<view class="page-container">
		<!-- 顶部导航栏 -->
		<view class="header">
			<x-status-bar></x-status-bar>
			<view class="flex-center-between header-navbar">
				<x-back-btn></x-back-btn>
				<view class="u-f-40 common-title">{{ lang_shop.writeTable }}</view>
				<view></view>
			</view>

			<view class="u-f-32 u-lh-44" style="padding: 0 42rpx;">
				<view class="flex-center-between">
					<view class="font-medio">{{ lang_shop.couponDetail }}</view>
					<view class="flex-center">
						<view @click="dateVisible = true">{{ dateValue }}</view>
						<image src="@/static/image/common/filter_btn.png" style="width: 88rpx; height: 88rpx;"
							class="clear-flex" @click="dateTypeVisible = true"></image>
					</view>
				</view>

				<view class="flex-center-around u-text-center">
					<view v-for="(item, index) in bases" :key="index">
						<view>
							<view class="u-f-36 u-lh-42 font-bold">{{ dataInfo[item.key] || 0 }}</view>
							<view class="u-m-t-4 u-f-24 u-lh-34 color-grew">{{ lang_shop[item.labelKey] }}</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 列表 -->
		<view>
			<mescroll-body height="calc(100vh - 304rpx - var(--status-bar-height))" ref="mescrollRef"
				@init="mescrollInit" @down="downCallback" @up="upCallback">
				<view>
					<view v-for="(item, index) in dataList" :key="index" style="padding: 20rpx 40rpx 0;">
						<coupon-card :item="({ ...item, couponType: 4 })"></coupon-card>
					</view>
				</view>
			</mescroll-body>
		</view>

		<x-popup v-model="dateTypeVisible" mode="bottom" z-index="9999999">
			<view class="datetype-container">
				<view v-for="(item, index) in dateTypes" :key="index">
					<u-gap v-if="index" height="0.5" bg-color="rgba(151,151,151,.2)"></u-gap>
					<view class="u-f-32 u-lh-44 flex-center-center" style="padding: 32rpx 0;"
						@click="changeDateType(item)">
						{{ lang_shop[item.key] }}
					</view>
				</view>

				<!-- 分割线 -->
				<u-gap height="8" bg-color="#F5F5F5"></u-gap>
				<view class="u-f-36 u-lh-50 flex-center-center" style="padding: 32rpx 0;"
					@click="dateTypeVisible = false">
					{{ lang_shop.cancel }}
				</view>
			</view>
		</x-popup>

		<!-- 时间选择器 -->
		<u-picker v-model="dateVisible" mode="time" z-index="10203" :params="params" :showTimeTag="false" @cancel="dateTypeVisible = true"
			@confirm="dateChange"></u-picker>
	</view>
</template>

<script>
	import {
		couponUserCouponPage,
		couponCouponData
	} from '@/api/merchant/coupon.js'
	import dayjs from 'dayjs'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";

	export default {
		mixins: [MescrollMixin], // 使用mixin
		data() {
			return {
				bases: [{
					key: 'totalUseCount',
						label: '总核销量',
						labelKey: 'allSale'
					},
					{
						key: 'useCount',
						label: '核销量',
						labelKey: 'saled'
					},
					{
						key: 'unUseCount',
						label: '未核销',
						labelKey: 'unSale'
					},
					{
						key: 'sellCount',
						label: '销量',
						labelKey: 'sale'
					}
				],
				dataInfo: {},
				dataList: [],

				dateTypes: [{
						label: '日期',
						key: 'day',
						value: 3
					},
					{
						label: '月份',
						key: 'month',
						value: 2
					},
					{
						label: '年份',
						key: 'year',
						value: 1
					},
				],
				dateTypeValue: 'day',
				dateTypeVisible: false,

				dateValue: dayjs().format('YYYY-MM-DD'),
				params: {
					year: true,
					month: true,
					day: true,
					hour: false,
					minute: false,
					second: false
				},
				dateVisible: false,
			}
		},
		// 获取类型
		onLoad({
			type
		}) {},
		methods: {
			changeDateType(item) {
				let {
					key
				} = item
				this.dateTypeValue = key
				this.params.month = ['month', 'day'].includes(key)
				this.params.day = key === 'day'
				this.dateTypeVisible = false
				this.dateVisible = true
			},
			dateChange(e) {
				let {
					year,
					month,
					day
				} = e
				let type = this.dateTypeValue
				this.dateValue = type === 'year' ? year :
					type === 'month' ? `${ year }-${ month }` :
					`${ year }-${ month }-${ day }`
				this.downCallback()
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				console.log(this.dateTypes)
				let dateType = (this.dateTypes.find(item => item.key === this.dateTypeValue) || {}).value
				let queryObj = {
					dateType,
					date: this.dateValue
				}
				couponCouponData(queryObj).then((data) => {
					this.dataInfo = {
						...data
					}
				})

				let node = this.$refs['mescrollRef']
				couponUserCouponPage({
					pageNumber: page.num,
					pageSize: page.size,
					...queryObj
				}).then(res => {
					node && node.mescroll.endSuccess(res.list.length);
					if (page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
					this.dataList = this.dataList.concat(res.list); //追加新数据
				}).catch(() => {
					node && node.mescroll.endErr();
				})
			},
			downCallback() {
				let node = this.$refs['mescrollRef']
				node && node.mescroll.resetUpScroll()
			}
		}
	}
</script>

<style scoped lang="scss">
	.header {
		padding-bottom: 38rpx;
		border-bottom-left-radius: 46rpx;
		border-bottom-right-radius: 46rpx;
		box-shadow: 0 2rpx 0 0 rgba(0, 0, 0, 0.07);
	}

	.scroll {
		&-container {
			height: calc(100% - var(--status-bar-height) - 305rpx);
		}
	}

	.datetype {
		&-container {
			border-top-left-radius: 16rpx;
			border-top-right-radius: 16rpx;
			width: 750rpx;
			background-color: #FFFFFF;
		}

		&-box {
			position: relative;
			top: -18rpx;
			left: 50%;
			transform: translateX(-50%);
			width: 690rpx;
		}
	}
</style>
